package com.example.luke.shengdangxiaoattendance;

import android.content.Context;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.example.luke.shengdangxiaoattendance.Common.CommonActivity;
import com.example.luke.shengdangxiaoattendance.Handler.ClassSetupHandler;
import com.example.luke.shengdangxiaoattendance.Model.App;
import com.example.luke.shengdangxiaoattendance.Model.PushBean;
import com.example.luke.shengdangxiaoattendance.Model.Setting;
import com.example.luke.shengdangxiaoattendance.Model.StudentAttendance;
import com.example.luke.shengdangxiaoattendance.Util.GlobalFunction;
import com.example.luke.shengdangxiaoattendance.http.HttpClient;
import com.example.luke.shengdangxiaoattendance.http.HttpResponseHandler;
import com.example.luke.shengdangxiaoattendance.http.NetWorkConstant;
import com.google.gson.Gson;

import org.json.JSONObject;

import java.io.IOException;
import java.sql.Time;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Timer;
import java.util.TimerTask;

import javax.microedition.khronos.opengles.GL;

import butterknife.ButterKnife;
import butterknife.InjectView;
import okhttp3.Headers;
import okhttp3.Request;

/**
 * Created by luke on 31/05/2017.
 */

public class DetailActivity extends CommonActivity {

    @InjectView(R.id.totalStudent)TextView totStudent;
    @InjectView(R.id.presentStudent)TextView preStudent;
    @InjectView(R.id.absentStudent)TextView abStudent;

    @InjectView(R.id.weekDay)TextView weekDay;
    @InjectView(R.id.currentTime)TextView currentTime;

    @InjectView(R.id.todayDate)TextView todayDate;

    @InjectView(R.id.name)TextView name;
    @InjectView(R.id.gender)TextView gender;
    @InjectView(R.id.className)TextView className;
    @InjectView(R.id.name2)TextView  name2;

     @InjectView(R.id.attendStat)TextView attendStat;

    @InjectView(R.id.commentNot)TextView commentNot;

    ClassSetupHandler classSetupHandler;

     boolean studentAttend=false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.center_detail_container);
        classSetupHandler=new ClassSetupHandler(this);


//


        try {


            initView();


        }catch (Exception e){

            GlobalFunction.printTextByTag(e.getMessage());
        }

        initWildDog();


    }
    public String getTime(){

        long time=  System.currentTimeMillis()/1000;//获取系统时间的10位的时间戳

        String  str=String.valueOf(time);

        return str;

    }

    @Override
    public void initView() {
        super.initView();

        ButterKnife.inject(this);
        currentTime.setText(GlobalFunction.getTimeWithFomat(new Date(), "HH:mm:ss"));
        todayDate.setText(GlobalFunction.getTimeWithFomat(new Date(), "MM月dd日"));

//        climate.setText(MainApp.getInstance().climate);
//        temperature.setText(MainApp.getInstance().temperature);
//        airDirection.setText(MainApp.getInstance().airDirection);



        weekDay.setText(GlobalFunction.getWeekDay());
        name.setText(App.getInstance().selectedStudent.name);

        gender.setText(App.getInstance().selectedStudent.gender);

        className.setText(App.getInstance().selectedStudent.className);
        totStudent.setText(App.getInstance().totalStudent);

       name2.setText(App.getInstance().currentCourse.name2);
        if (name2.getText().length()>50){
            String temp=name2.getText().toString().substring(0,name2.length()/2);
            name2.setText(temp+"......");
        }


     studentAttend=StudentAttendance.checkStudentAttend(this,App.getInstance().selectedStudent.className
             ,GlobalFunction.getTimeWithFomat(new Date(),"yyyy-MM-dd"),App.getInstance().selectedStudent.employeeID,App.getInstance().currentCourse.Time);

        int curPreCount=Integer.valueOf(App.getInstance().preStudent);

      if (studentAttend){

          attendStat.setText("已签到！");

          playDetMusicFile("absent_done");

      }else {


          if (checkIsLate()){

              if (!Setting.defaultClassName.equals("报到大厅")){

                  attendStat.setText("已迟到！");
                  playDetMusicFile("absent_late");

              }else {

                  playDetMusicFile("absent_success");
              }

         checkUnrateDKW();


          }else {

            playDetMusicFile("absent_success");

          }

          saveAttendance();

          curPreCount=curPreCount+1;//


      }


        reloadAttendCount(curPreCount);


    GlobalFunction.printTextByTag("unrated:"+App.getInstance().selectedStudent.unratedKW+"");

    }
    private void checkUnrateDKW(){


        if (Integer.valueOf(App.getInstance().selectedStudent.unratedKW)>=1){

            commentNot.setVisibility(View.VISIBLE);

        }

        int temp=Integer.valueOf(App.getInstance().selectedStudent.unratedKW);

        switch (temp){

            case 0:



                break;

            case 1:
                commentNot.setText("您有一门课程未点评，请及时点评");

                break;

            case 2:
                commentNot.setText("您有两门课程未点评，请及时点评");

                break;

          default:
            {

               commentNot.setText("您有三门课程未点评，请及时点评，累计三门未点评考勤签到无效");
                playDetMusicFile("comment_course");
            }
                break;

        }
    }



    MediaPlayer mediaPlayer2;
    private    void playDetMusicFile(String fileName){


        if (mediaPlayer2!=null){
            mediaPlayer2.stop();



        }



        try {







            int resID = getResources().getIdentifier(fileName, "raw", context.getPackageName());

            mediaPlayer2 = MediaPlayer.create(DetailActivity.this, resID);


            mediaPlayer2.start();

            mediaPlayer2.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        backToMain();
                    }
                });





                }
            });





        }
        catch (Exception e){
            e.printStackTrace();
        }


    }

    private  void reloadAttendCount(int curPreCount){
        GlobalFunction.printTextByTag("preCount:"+curPreCount);

        preStudent.setText(curPreCount+"");
        Integer abStudentCount=Integer.valueOf(App.getInstance().totalStudent)-curPreCount;

        abStudent.setText(abStudentCount+"");
    }


    public  void  backToMain(){

        Timer timer=new Timer();
        timer.schedule(new Task(),1000);
    }

    private  class Task extends TimerTask{
        @Override
        public void run() {


        finish();

        }
    }

    private void saveAttendance(){

     if (!StudentAttendance.checkStudentAttend(this,App.getInstance().selectedStudent.className
                ,GlobalFunction.getTimeWithFomat(new Date(),"yyyy-MM-dd"),App.getInstance().selectedStudent.employeeID,App.getInstance().currentCourse.Time)){
        StudentAttendance studentAttendance=new StudentAttendance();
        studentAttendance.name=App.getInstance().selectedStudent.name;
        studentAttendance.className=App.getInstance().selectedStudent.className;
        studentAttendance.employeeID=App.getInstance().selectedStudent.employeeID;
        studentAttendance.attendanceDate=GlobalFunction.getTimeWithFomat(new Date(),"yyyy-MM-dd");
        studentAttendance.attendanceTime=GlobalFunction.getTimeWithFomat(new Date(),"HH:mm:ss");
        studentAttendance.classTime=App.getInstance().currentCourse.Time;
        studentAttendance.isPush="0";//科室名称
         studentAttendance.remark=App.getInstance().selectedStudent.kwName;

        StudentAttendance.saveAttendance(this,studentAttendance);
    }



    }

    private  boolean checkIsLate(){

       long min= GlobalFunction.minDiff(new Date(),App.getInstance().currentCourse.getStartTime());



        return new Date().after(App.getInstance().currentCourse.getStartTime()) && min>=10;



    }






}
